package com.martin.fastframe.ui.dialog

import android.content.Context
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.KeyboardUtils
import com.martin.fastframe.R
import com.martin.fastframe.databinding.DialogCommentInputBinding
import com.martin.fastframe.ui.holder.EmojiViewHolder
import com.martin.lib_base.base.BaseDialogModel
import com.martin.lib_base.bean.EmojiBean
import com.martin.lib_base.constant.ConstantKey
import com.martin.lib_base.interfaces.IOnItemClickListener
import com.martin.lib_base.ktx.*
import com.tencent.mmkv.MMKV
import dev.utils.app.ViewUtils

class CommentInputDialogModel(
    //发送功能
    private val sendCallBack: (data: String) -> Unit = {}
) : BaseDialogModel<DialogCommentInputBinding>() {

    //输入的内容
    val inputData = MutableLiveData("")

    //键盘是否展示
    private var keyBoardIsShow = false

    //表情视图持有
    val emojiViewHolders = listOf(EmojiViewHolder::class.java)

    override fun layoutId() = R.layout.dialog_comment_input

    override fun onBuild(builder: XPopup.Builder) {
        builder.moveUpToKeyboard(false)
            .autoOpenSoftInput(true)
            .autoFocusEditText(true)
    }

    override fun onDialogCreated(context: Context) {
        binding.apply {
            model = this@CommentInputDialogModel
            //设置表情点击事件
            rv.addOnItemClickListener(object : IOnItemClickListener {
                override fun onItemClick(position: Int, type: Int) {
                    val item = rv.commonAdapter()!!.getItem(position) as EmojiBean
                    et.text?.insert(et.selectionStart, item.parseToKey())
                    loge("表情转换后的字符串  ${item.parseToKey()}")
                }
            })

            //获取保存的键盘高度
            val keyBoardHeight = MMKV.defaultMMKV().getInt(ConstantKey.KEYBOARD_HEIGHT, 0)
            if (keyBoardHeight > 0) {
                ViewUtils.setHeight(rv, keyBoardHeight)
            }
            root.context.getActivity()?.let {
                //监听键盘展示
                KeyboardUtils.registerSoftInputChangedListener(
                    it.window, dialog
                ) { height ->
                    rv.post {
                        keyBoardIsShow = height > 0
                        if (keyBoardIsShow && rv.height != height) {
                            ViewUtils.setHeight(rv, height)
                            MMKV.defaultMMKV().putInt(ConstantKey.KEYBOARD_HEIGHT, height)
                        }
                    }
                }
            }
        }

        //获取并刷新表情
        val emojiList =
            context.resources.getIntArray(R.array.emoji_code).toList()
                .map {
                    EmojiBean(it)
                }
        binding.rv?.refresh(emojiList)
    }

    /**
     * 表情点击
     */
    fun emojiClick(view: View) {
        showHideKeyBoard(!keyBoardIsShow)
    }

    /**
     * '@'点击
     */
    fun atClick(view: View) {
        showToast("@点击事件")
    }

    /**
     * 发送点击
     */
    fun sendClick(view: View) {
        if (inputData.value.isNullOrEmpty()) {
            showToast("请输入评论内容")
            return
        }
        sendCallBack(inputData.value ?: "")
        inputData.value = ""
    }

    /**
     * 展示或隐藏键盘
     */
    private fun showHideKeyBoard(show: Boolean) {
        loge("操作键盘  是否展示  $show")
        binding.et?.apply {
            post {
                if (show) {
                    KeyboardUtils.showSoftInput(this)
                } else {
                    KeyboardUtils.hideSoftInput(this)
                }
            }
        }
    }

}